$(function () {
    // 选择日期
    var format = 'YYYY-MM-DD';
    $('#datePickerInput').daterangepicker({
        'showDropdowns': true,
        'autoApply': true,
        'dateLimit': {
            'days': 30
        },
        'locale': {
            'format': format,
            'applyLabel': lang('date.ensure'),
            'cancelLabel': lang('date.cancel'),
            'daysOfWeek': [
                lang('week.7'),
                lang('week.1'),
                lang('week.2'),
                lang('week.3'),
                lang('week.4'),
                lang('week.5'),
                lang('week.6')
            ],
            'monthNames': [
                lang('month.1'),
                lang('month.2'),
                lang('month.3'),
                lang('month.4'),
                lang('month.5'),
                lang('month.6'),
                lang('month.7'),
                lang('month.8'),
                lang('month.9'),
                lang('month.10'),
                lang('month.11'),
                lang('month.12')
            ],
            'firstDay': 1
        },
        'linkedCalendars': false,
        'parentEl': 'body',
        'startDate': $('#datePicker').find('.start').html(),
        'endDate': $('#datePicker').find('.end').html(),
        'minDate': '2000-01-01',
        'maxDate': '2038-01-01',
        'opens': 'left'
    }, function (start, end, label) {
        $('#datePicker').find('.start').html(start.format(format));
        $('#datePicker').find('.end').html(end.format(format));
        refreshPage();
    });
    $('#datePickerContainer').on('click', function () {
        $('#datePickerInput').trigger('focus');
    });
    var refreshPage = function () {
        var startDate = $('#datePicker').find('.start').html();
        var endDate = $('#datePicker').find('.end').html();
        var param = {};
        param.hotel = window.currentHotel;
        param.start = startDate;
        param.end = endDate;
        showContentLoading();
        ajaxRequest('/Home/Report/getCharity', param, 'post', function (data) {
            var info = data.data;
            var orderInfos = info.order;
            var currentPage = info.page.current;
            var pages = info.page.pageTotal;
            var total = info.page.total;
            var totalCharity = info.totalCharity;

            var viewCharityPrice = $('#viewCharityPrice');
            viewCharityPrice.find('span[data-tag="surplus"]').html(totalCharity);
            // 表格渲染
            refreshTable(orderInfos);
            refreshPageFooter(currentPage, pages, total);
            hideContentLoading();
        }, function (error) {
            hideContentLoading();
            if (error.responseJSON !== undefined) {
                modalAlert(error.responseJSON.info);
            }
        });
    };

    var refreshTable = function (orderInfos) {
        var trs = '';
        var templateOriginal = $('#templateOrderList');
        $.each(orderInfos, function (index, info) {
            var template = $(templateOriginal.clone().html());
            template.find('[data-tag="number"]').html(info.orderNumber);
            template.find('[data-tag="name"]').html(info.name);
            template.find('[data-tag="mobile"]').html(info.mobile);
            template.find('[data-tag="receipt"]').html(info.receipt);
            template.find('[data-tag="charity"]').html(info.charity);
            template.find('[data-tag="ordertime"]').html(info.orderTime);
            template.find('[data-tag="status"]').html(info.status);
            trs += template.prop('outerHTML');
        });
        $('#orderListBody').html(trs);
    };

    var refreshPageFooter = function (currentPage, pages, total) {
        currentPage = parseInt(currentPage);
        pages = parseInt(pages);
        total = parseInt(total);
        var pageItems = '';
        for (var i = 1; i <= pages; i++) {
            if (i === currentPage) {
                pageItems += '<li class="active"><a href="javascript:void(0);" data-value="' + i + '">' + i + '</a></li>';
            } else {
                pageItems += '<li><a href="javascript:void(0);" data-value="' + i + '">' + i + '</a></li>';
            }
        }
        $('#charityView').find('.pagination').html(pageItems);
        $('#charityView').find('.content-panel-pagination-count').html(total);
    };
    // 点击分页地址
    $('#charityView .pagination').on('click', 'li:not(.active)>a', function (e) {
        e.preventDefault();
        var pageLink = $(this);
        var param = {};
        param.hotel = window.currentHotel;
        param.current = pageLink.data('value');
        param.start = $('#datePicker').find('.start').html();
        param.end = $('#datePicker').find('.end').html();
        $('#charityView .content-panel-loading').removeClass('hidden');
        ajaxRequest('/Home/Report/getCharity', param, 'post', function (data) {
            var info = data.data;
            var orderInfos = info.order;
            var currentPage = info.page.current;
            var pages = info.page.pageTotal;
            var total = info.page.total;
            var totalCharity = info.totalCharity;

            var viewCharityPrice = $('#viewCharityPrice');
            viewCharityPrice.find('span[data-tag="surplus"]').html(totalCharity);

            // 表格渲染
            refreshTable(orderInfos);
            refreshPageFooter(currentPage, pages, total);
            $('#charityView .content-panel-loading').addClass('hidden');
        }, function (error) {
            if (error.responseJSON !== undefined) {
                modalAlert(error.responseJSON.info);
            }
        });
    });

    var showContentLoading = function () {
        $('.content-panel .content-panel-loading').removeClass('hidden');
    };
    var hideContentLoading = function () {
        $('.content-panel .content-panel-loading').addClass('hidden');
    };
    refreshPage();
});
